package com.threeonemeeting.adapter;

import java.net.URLEncoder;
import java.util.ArrayList;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.threeonemeeting.Const;
import com.threeonemeeting.R;
import com.threeonemeeting.listcache.MeetingInfoCache;
import com.threeonemeeting.model.MeetingInfo;
import com.threeonemeeting.util.MiscUtil;

public class MeetingInfoListAdapter extends ArrayAdapter<MeetingInfo> {

	protected ArrayList<MeetingInfo> mMeetingInfoList = null;

	public MeetingInfoListAdapter(Activity activity) {
		super(activity, 0);
	}

	public void setCommentList(ArrayList<MeetingInfo> messageList) {
		mMeetingInfoList = messageList;
		notifyDataSetChanged();
	}

	public ArrayList<MeetingInfo> getMessageList() {
		return mMeetingInfoList;
	}

	@Override
	public void add(MeetingInfo messageInfo) {
		if (mMeetingInfoList == null)
			mMeetingInfoList = new ArrayList<MeetingInfo>();

		mMeetingInfoList.add(messageInfo);
	}

	@Override
	public void clear() {
		if (mMeetingInfoList != null) {
			mMeetingInfoList.clear();
		}
	}

	@Override
	public int getCount() {
		if (mMeetingInfoList == null)
			return 0;

		return mMeetingInfoList.size();
	}

	@Override
	public MeetingInfo getItem(int position) {
		return mMeetingInfoList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {

		Activity activity = (Activity) getContext();
		MeetingInfo meetingInfoItemData = getItem(position);

		View rowView = convertView;
		MeetingInfoCache viewCache;
		if (rowView == null) {
			LayoutInflater inflater = activity.getLayoutInflater();
			rowView = inflater.inflate(R.layout.lvi_meeting, null);
			viewCache = new MeetingInfoCache(rowView, meetingInfoItemData.nId);
			rowView.setTag(viewCache);
		} else {
			viewCache = (MeetingInfoCache) rowView.getTag();
		}

		// 会议标题
		TextView meetingTitleView = viewCache.getMeetingTitleTextView();
		String strMeetingTitle = meetingInfoItemData.strTitle;
		meetingTitleView.setText(strMeetingTitle);

		// 会议图片
		ImageView meetingImageView = viewCache.getMeetingImageView();
		String strThumbUrl = Const.URL.BASE_SERVER_URL;
		if (meetingInfoItemData.strOrginalImageUrl != null
				&& meetingInfoItemData.strOrginalImageUrl.equalsIgnoreCase("") == false) {
			strThumbUrl += URLEncoder
					.encode(meetingInfoItemData.strOrginalImageUrl);
			MiscUtil.setImageView(meetingImageView, strThumbUrl);
		} else {
			meetingImageView
					.setBackgroundResource(R.drawable.default_meeting_image);
		}

		// 会议的时间
		TextView meetingTimeView = viewCache.getMeetingTimeTextView();
		meetingTimeView.setText(meetingInfoItemData.strTime);
		
		// 会议是否免费
		TextView meetingTypeView = viewCache.getMeetingTypeTextView();
		if (meetingInfoItemData.bFree == true) {
			meetingTypeView.setText("免费会议");

		} else {
			meetingTypeView.setText("收费会议");
		}

		// 会议是研讨会还是其余
		TextView meetingCategoryView = viewCache.getMeetingCategoryTextView();
		meetingCategoryView.setText(meetingInfoItemData.strType);

		// 地址
		TextView meetingAddressView = viewCache.getMeetingAddressTextView();

		if (meetingAddressView != null) {
			meetingAddressView.setText(meetingInfoItemData.strAddress);
		}

		return rowView;
	}

}
